package com.saasovation.common.port.adapter.notification;

import com.saasovation.common.notification.Notification;
import com.saasovation.common.notification.NotificationSerializer;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class SpringMessageUtils {

    public static Message convertToMessage(Notification notification) {
        String notificationJson =
                NotificationSerializer.instance().serialize(notification);
        Charset utf8 = StandardCharsets.UTF_8;
        MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding(utf8.name())
                .setMessageId(String.valueOf(notification.notificationId()))
                .setType(notification.typeName())
                .setTimestamp(notification.occurredOn())
                .build();
        byte[] contentBytes = notificationJson.getBytes(utf8);
        return new Message(contentBytes, messageProperties);
    }
}
